<?php
require "../inc/rwINIfile.php";

$shareDir = "/var/shares/";
$shareList = array();


function createSharelist()
{
        global $shareDir;
        global $shareList;
        $shareList = array();

        $dh  = opendir($shareDir);
        while (false !== ($thing = readdir($dh))) {
                if (is_dir("$shareDir$thing") && $thing != "." && $thing != "..") {
                        $arr = array();
                        $arr["owner"] = fileowner("$shareDir$thing");
                        $arr["grp"] = filegroup("$shareDir$thing");
                        $shareList[$thing] = $arr;
                }
        }
}


function acl_edit() {
	global $shareDir;
	global $shareList;

	$name = $_POST["name"];
	$share = $shareList["$name"];

	//Ändra filer och kataloger rekrusivt
	foreach($_POST as $key => $value) {
		if( preg_match('/^perm_(.*)/', $key, $match) ) {
			$rule = $match[1].$value;
			exec("/usr/bin/setfacl -R -m $rule " . $shareDir.$name);
			exec("/usr/bin/setfacl -R -m d:$rule " . $shareDir.$name);
		}
	}
	info('acl_changed');
}


function acl_add() {
	global $shareDir;
	global $shareList;

	$name = $_POST["name"];
	$share = $shareList["$name"];

	$rule = intval( substr($_POST['new'], 1) );
	$perm = $_POST['permnew'];

	if( $_POST['new'][0] == 'u' ) {
		exec("/usr/bin/setfacl -R -m u:$rule:$perm " . $shareDir.$name);
		exec("/usr/bin/setfacl -R -m d:u:$rule:$perm " . $shareDir.$name);
		info('acl_changed');
	}
	else if( $_POST['new'][0] == 'g' ) {
		exec("/usr/bin/setfacl -R -m g:$rule:$perm " . $shareDir.$name);
		exec("/usr/bin/setfacl -R -m d:g:$rule:$perm " . $shareDir.$name);
		info('acl_changed');
	}
}


function acl_remove() {
	global $shareDir;
	global $shareList;

	if( !isset($_GET["show"]) || !isset($_GET['removeacl']) ) {
		echo "\nFEL: indata saknas";
		die;
	}

	$name = $_GET["show"];
	$share = $shareList["$name"];
	$rule = $_GET['removeacl'];

	$arr = explode(':',$rule);
	//Om ägarens rättigheter ska ändras
	if( $arr[0] == 'user' && $arr[1] == fileowner($shareDir.$name) ) {
		exec("/usr/bin/setfacl -R -m u::0 " . $shareDir.$name);
		exec("/usr/bin/setfacl -R -m d:u::0 " . $shareDir.$name);
		exec("/bin/chown -R root " . $shareDir.$name);	
	}
	//Om ägande grupps rättigheter ska ändras
	if( $arr[0] == 'group' && $arr[1] == filegroup($shareDir.$name) ) {
		exec("/usr/bin/setfacl -R -m g::0 " . $shareDir.$name);
		exec("/usr/bin/setfacl -R -m d:g::0 " . $shareDir.$name);
		exec("/bin/chgrp -R root " . $shareDir.$name);
	}

	//Sätt de nya rättigheterna
	exec("/usr/bin/setfacl -R -x $rule " . $shareDir.$name);
	exec("/usr/bin/setfacl -R -x d:$rule " . $shareDir.$name);

        info('acl_changed');
}


function create() {
		global $shareDir;
		global $shareList;

		$name = $_POST["new"];
		$share = $shareList[$name];

		mkdir("$shareDir$name");

		//Ändra filer och kataloger rekrusivt
		exec("/bin/chown -R root " . $shareDir.$name);
		exec("/bin/chgrp -R root " . $shareDir.$name);
		exec("/bin/chmod -R g+s " . $shareDir.$name);
		exec("/usr/bin/setfacl -m u::rwX,g::rwX,o::rwX " . $shareDir.$name);
		exec("/usr/bin/setfacl -m d:u::rwX,d:g::rwX,d:o::rwX " . $shareDir.$name);

		// lägger till i shares.conf
		$ini = "/etc/samba/shares.conf";
		if( is_file($ini) || is_link($ini) )
			$ini_array = parse_ini_file($ini, true);
		else
			$ini_array = array();
		if (!in_array($name,$ini_array)) {
                	$ini_array[$name] = array("path" => "/var/shares/$name", "browsable" => 1, "writeable" => 1, "inherit owner" => 1, "inherit permissions" => 1, "force directory mode" => "2000");
		}
		write_ini_file($ini,$ini_array);


		info('map_created');
}


function remove() {
        global $shareDir;
        global $shareList;

        if(!isset($shareList[$_GET["remove"]])) {
                error('map_dont_exist');
                return 1;
        }
        $name = $_GET["remove"];

        rmDirR("$shareDir$name");

        // tar bort från shares.conf
        $ini = "/etc/samba/shares.conf";
        $ini_array = parse_ini_file($ini, true);
        unset($ini_array[$name]);
        write_ini_file($ini,$ini_array);

        info('map_removed');
}


////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////

createSharelist();

/* Listar alla delningar */

//om knapp för ny delning
if( isset($_POST['b_new']) && isset($_POST["new"]) ) {
	if( trim($_POST["new"]) == "" )
		return;  // tomt namn, ignorera
	else if( isset( $shareList[ trim($_POST["new"]) ] ) )
		error('map_exist');
	else if( !allowedCharacters(trim($_POST["new"])))
                error('map_reg');
	else {
		create();
		$GET['show'] = trim($_POST["new"]);
	}
}
//om knapp för att ta bort delad katalog
else if( isset($_GET['remove']) ) {
	remove();
}


/* Visar rättigheter för delning */

else if( isset($_POST['b_create']) ) {
	//regel ska skapas
	acl_add();
	$GET['show'] = $_POST['name'];
}
else if( isset($_GET['removeacl']) ) {
	//regel ska tas bort
	acl_remove();
	$GET['show'] = $_GET['show'];
}
else {
	//regler ska ändras/sättas på nytt
	acl_edit();
	$GET['show'] = $_POST['name'];
}
?>
